<?php
/**
 * 内核容器
 *
 * @category     Shuxun Cms
 * @date         2015年10月4日
 * @author       Shuxun Team <kaiping.jiang@scshux.com>
 * @file         Core.php
 */
namespace  Scshux\Plugin;
use Phalcon\Di\FactoryDefault;
use Scshux\Plugin\Activity;
use Scshux\AbstractPlugin;
use Scshux\Utils\Time;
use Scshux\Mvc\SModel;
class Core extends  AbstractPlugin
{

	/**
	 * auth
	 * @var \Scshux\Plugin\Auth
	 */
	public   $auth = null;

	protected  $di   = null;

	protected  $activityObj = null;

	protected  $siteID   = null;

	/**
	 * site
	 * @var \Scshux\Plugin\Site
	 */
	public   $site = null;

	public  function  __construct()
	{
		$this->di   = FactoryDefault::getDefault();
		$this->auth = new  Auth();
		$this->site = new  Site();
	}



	/**
	 * 判断是否微信
	 * @return boolean
	 */
	public  function isWechat()
	{
		if(strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false ){
			return true;
		}
		return false;
	}

	/**
	 * 获取cache
	 * @return \Phalcon\Cache\BackendInterface
	 */
	public  function  getCahce()
	{
		return $this->getDI()->get('cache');
	}


	/**
	 * 获取日志
	 * @return \Phalcon\Logger\Adapter\File
	 */
	public   function getLogger()
	{
		return $this->getDI()->get('logger');
	}



	/**
	 * 获取活动管理对象
	 * @return \Scshux\Plugin\Activity
	 */
	public  function  getActivity()
	{
		if($this->activityObj == null){
			$this->activityObj = new Activity();
		}
		return $this->activityObj;
	}

	/**
	 * 获取当前站点
	 */
	public  function getSiteID()
	{
		if(!empty($this->siteID)){
			return intval($this->siteID);
		}

		$siteID = $this->request->get('__siteid','int',0);
		if(empty($siteID)){
			$siteID = $this->cookies->get('__siteid')->getValue();
		}else{
			$this->cookies->set('__siteid',$siteID,Time::gmtime() + 30 * 86400,'/');
		}
		
		if($siteID){
			return intval($siteID);
		}else{
			return 1;
		}
	}
}